package com.atom.process.cnmuseum.cj.service;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;

import com.atom.common.VariablesConstant;
import com.atom.common.exception.AvensNullPointerException;
import com.atom.common.remote.user.UserUtil;
import com.atom.common.util.PreConditionUtil;
import com.atom.process.cnmuseum.cj.tool.ProcessDefinitions;
import com.atom.process.cnmuseum.cj.tool.TaskDefinitions;
import com.atom.process.cnmuseum.cj.vo.request.GcxxRequestVO;
import com.atom.process.cnmuseum.cj.vo.request.RequestVO;
import com.atom.process.cnmuseum.cj.vo.request.WhrlxxxRequestVO;
import com.atom.process.cnmuseum.cj.vo.request.XzxxRequestVO;
import com.atom.process.cnmuseum.cj.vo.request.YxxxRequestVO;
import com.sinosoft.cnmuseum.utils.AuthorityUtil;
import com.sinosoft.cnmuseum.utils.LogUtils;

@Service
public class CaijiService extends AbstractService implements ServiceInterface {

    public synchronized String create(UserUtil user, String title) {
        Map<String, Object> var_start = new HashMap<String, Object>();
        // 指定起草人
        var_start.put("nextAssignee", user.getUserid());
        // 启动流程
        String processInstanceId = activitiHandler.startProcessInstanceByKey(ProcessDefinitions.PROCESS_DEF_KEY, var_start);
        // 生成标题
        String biaoTi = "采集_" + title;
        // 初始化流程变量
        activitiHandler.initProcessVariables(ProcessDefinitions.PROCESS_DEF_KEY, processInstanceId, user, biaoTi, null);
        /* 初始化流程数据 */
        // 创建PDO
        createPdo(user, processInstanceId, biaoTi);
        // 获取申请任务
        Task task = activitiHandler.findTaskByProcessIdByTaskDefinitionKey(processInstanceId, TaskDefinitions.采集.getKey());
        if (task != null) {
            String taskId = task.getId();
            // 创建ADO
            createAdo(user, processInstanceId, taskId, biaoTi);

            // 创建流程
            activitiHandler.createRemoteRunning(taskId, processInstanceId, user);
            return taskId;
        } else {
            throw new AvensNullPointerException("task is null where processInstanceId = "
                    + processInstanceId + " and taskDefinitionKey = "
                    + TaskDefinitions.采集.getKey());
        }
    }

    @Override
    public void save(RequestVO vo, GcxxRequestVO gcxxvo, XzxxRequestVO xzxxvo, YxxxRequestVO yxxxvo, WhrlxxxRequestVO whrlxxxvo,  UserUtil user) {
        /* 参数校验 */
        paramsCheck(vo);

        String taskId = vo.getTaskId();
        String processInstanceId = vo.getProcessId();
		/* 处理流程数据 */
    	String onetitle = "采集" + gcxxvo.getCollection_code();
        // 更新ADO
        saveAdo(taskId, vo, gcxxvo, xzxxvo, yxxxvo,whrlxxxvo,user,onetitle);
        // 创建流程
        activitiHandler.createRemoteRunning(taskId, processInstanceId, user);
        // 更新流程变量
        // 生成标题
        String title = "采集_" + gcxxvo.getCollection_code();
        activitiHandler.updateProcessInstanceName(taskId, processInstanceId, title);
    }

    @Override
    public void submit(RequestVO vo, UserUtil user) {
		/* 参数校验 */
        paramsCheck(vo);
        String taskId = vo.getTaskId();
        String mainProcessInstanceId = vo.getProcessId();
        // 完成任务
        Map<String, Object> var_complete = new HashMap<String, Object>();
        var_complete.put(VariablesConstant.PREVIOUS_ASSIGNEE, user.getUserid());
        // 提交录入或审核
        String next = vo.getNext();
        if (next.equals("lr")) {
            String nextids = vo.getNextids();
            var_complete.put("taskFlowType", next);
            var_complete.put("nextAssignee", nextids);
        } else if (next.equals("sh")) {//内容审核
            String nextids = AuthorityUtil.getUseridsByJsid(AuthorityUtil.jsid_cj_nrshr);

            var_complete.put("taskFlowType", next);
            var_complete.put("nextAssignee", nextids);
        }

		/* 处理流程数据 */
        // 更新流程变量
        // 生成标题
        String title = vo.getBiaoti();
        activitiHandler.updateProcessInstanceName(taskId, mainProcessInstanceId, title);

        // 更新PDO
        ADO2PDO(mainProcessInstanceId, vo.getTaskId());
		commentDao.saveComment(vo.getYourAdvice(), user, null, 0, taskId, mainProcessInstanceId, TaskDefinitions.采集.getKey());
		/* 处理流程数据 */
        // 完成任务/写入已办表/删除待办表
        activitiHandler.completeTask(taskId, var_complete);
        //生成日志
        LogUtils logUtils = new LogUtils();
        String desc = title+LogUtils.MbXtglSJCJ_SUBMIT_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_submit), LogUtils.model_sjcj, user.getUserid(), desc);
   
    }

    public void delete(RequestVO vo, UserUtil user) {
		/* 参数校验 */
        paramsCheck(vo);
        String taskId = vo.getTaskId();
        String processInstanceId = vo.getProcessId();
        activitiHandler.deleteProcess(processInstanceId, taskId);
        //生成日志
        LogUtils logUtils = new LogUtils();
        String desc = vo.getBiaoti()+LogUtils.MbXtglSJCJ_DELETE_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_delete), LogUtils.model_sjcj, user.getUserid(), desc);
        
    }

    private void paramsCheck(RequestVO vo) {
        PreConditionUtil.checkNotNull(vo.getTaskId(), "taskId is null");
    }

}
